<template>
  <el-container style="height: 98vh;">
    <el-header>
      <div style="width:30%;float:left">
        <el-popover
          placement="right"
          trigger="click">
          <div class = "menu">
            <el-button @click="reset">修改密码</el-button>
            <el-button @click="logout">退出登录</el-button>
          </div>
          <i class = "el-icon-user" slot="reference" style="font-weight:blod;font-size:40px;margin-top:8px"></i>
        </el-popover>
      </div>
    </el-header>
    <el-container class = "cont">
      <el-aside width="200px" style="background-color: #333744;" >
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" @open = "ChangeSubMenu" :unique-opened="true" :collapse-transition = "false" :router="true" :default-active="activePath">
          <el-submenu :index="item.path+''" v-for = '(item,i) in list' :key = 'i' >
            <template slot="title">
              <span>{{item.name}}</span>
            </template>
            <el-menu-item :index="'/'+item.path+'/'+subItem.path" v-for = '(subItem,i) in item.children' :key = 'i' @click = "saveNavState('/'+item.path+'/'+subItem.path)">
              <template>
                <i class="el-icon-menu"></i>
                <span>{{subItem.name}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
      <router-view/>
    </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      activePath: '',
      list: [],
      isCollapse: false
    }
  },
  methods: {
    ChangeSubMenu () {
      window.sessionStorage.removeItem('ActivityName')
    },
    reset () {
      this.$router.push('/reset')
      window.sessionStorage.removeItem('activePath')
    },
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/')
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    async getList () {
      await this.$http.get('rest/List').then(res => {
        if (res.status === 200) {
          this.list = res.data
        }
      }).then(async () => {
        await this.$http.get('rest/Cate').then(res => {
          if (res.status === 200) {
            res.data.forEach((item, index, arr) => {
              arr[index].inputVisible = false
              arr[index].inputValue = ''
            })
            this.$store.commit('Cate', res.data)
          }
        })
      }).then(async () => {
        await this.$http.get('rest/Item').then(res => {
          if (res.status === 200) {
            this.$store.commit('Item', res.data)
          }
        })
      }).then(async () => {
        await this.$http.get('rest/Skill').then(res => {
          if (res.status === 200) {
            this.$store.commit('Skill', res.data)
          }
        })
      }).then(async () => {
        await this.$http.get('rest/Insc').then(res => {
          if (res.status === 200) {
            this.$store.commit('Insc', res.data)
          }
        })
      }).then(async () => {
        await this.$http.get('rest/Hero').then(res => {
          if (res.status === 200) {
            this.$store.commit('Hero', res.data)
          }
        })
      })
    }
  },
  created () {
    this.getList()
    this.activePath = this.$route.path
  },
  watch: {
    '$route.path': function () {
      this.activePath = window.sessionStorage.getItem('activePath')
    }
  }
}
</script>

<style scoped>

  .el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
    width: 1920px;
  }

.el-aside{
  height: 877px;
  background-color: #333744;
}
.el-main{
  height: 877px;
  padding:0
}
.cont{
  width: 1920px;
  height: 860px;
}
.el-menu{
  border-right: none;
}
</style>
